import LoadManager from "../../manager/LoadManager";
import ItemData from "../../data/ItemData";
import KnapsackModel from "../../model/KnapsackModel";
import ItemBox from "../../common/ItemBox";

const {ccclass, property} = cc._decorator;

@ccclass
export default class KnapsackView extends cc.Component {
    @property (cc.ScrollView)
    ItemView:cc.ScrollView = null;
    @property (cc.Button)
    btn_use:cc.Button = null;
    @property(cc.Button)
    btn_lost:cc.Button = null;
    @property (cc.Button)
    btn_depot:cc.Button = null; 
    @property(cc.Sprite)
    img_select:cc.Sprite = null;

    private  selectItemBox :cc.Node;//选定节点
    private itemBoxList:Array<cc.Node> = [];
    private resetItemBoxList() {
        let prefab :cc.Prefab = LoadManager.instance.getPrefabResByUrl("prefab/common/ItemBox");
        let itemDataList:Array<ItemData> = KnapsackModel.instance.itemDataList
        if (itemDataList.length>this.itemBoxList.length)
        {
            let len:number = itemDataList.length-this.itemBoxList.length
            for (let index = 0; index < len; index++) {
                let itemBox :cc.Node = cc.instantiate(prefab);
                this.ItemView.content.addChild(itemBox);
                this.itemBoxList.push(itemBox)
                itemBox.on(cc.Node.EventType.TOUCH_END,this.onSelect,this)
            }
        }
        else
        {
            let len:number = this.itemBoxList.length-itemDataList.length
            for (let index = 0; index < len; index++) {
                for (const key in this.itemBoxList) {
                    if (this.itemBoxList.hasOwnProperty(key)) {
                        const itemBox = this.itemBoxList[key];
                        itemBox.destroy()
                        this.itemBoxList.splice(Number(key),1)
                        break
                    }
                }
            }
        }
        for (let index = 0; index < itemDataList.length; index++) {
            const itemData:ItemData = itemDataList[index];
            const itemBox:cc.Node = this.itemBoxList[index];
            itemBox.x = itemBox.width/2+(index%4)*(itemBox.width+3);
            itemBox.y = -itemBox.height/2-Math.floor(index/4)*(itemBox.height+10);
            itemBox.getComponent(ItemBox).itemData = itemData;
        }
        this.img_select.node.active=false;
        this.selectItemBox = null;
    }
    //选定
    public onSelect(event:cc.Event.EventTouch) {
        this.img_select.node.active=true;
        let node :cc.Node = event.getCurrentTarget();
        this.selectItemBox = node;
        this.img_select.node.setPosition(this.selectItemBox.x,this.selectItemBox.y);
        this.ItemView.content.insertChild(this.img_select.node,this.itemBoxList.length+1);
    }

    //使用
    private onUseItem() {
        if (!this.selectItemBox)
        {
            return
        }
        let itemId:number = this.selectItemBox.getComponent(ItemBox).itemData.itemId
        KnapsackModel.instance.actionUseItem(itemId,1,function(nowdata:ItemData) {
            this.resetItemBoxList()
        }.bind(this))
    }
    //丢弃
    private onLostItem() {
        if (!this.selectItemBox)
        {
            return
        }
        let itemId:number = this.selectItemBox.getComponent(ItemBox).itemData.itemId
        KnapsackModel.instance.actionLostItem(itemId,1,function (nowdata:ItemData) {
            this.resetItemBoxList()
        }.bind(this));
        
    }
    //去仓库
    private onDepot() {
        if (!this.selectItemBox)
        {
            return
        }
    }
    start () {
        this.img_select.node.active = false;
        this.resetItemBoxList();
        this.btn_use.node.on(cc.Node.EventType.TOUCH_END,this.onUseItem,this);
        this.btn_lost.node.on(cc.Node.EventType.TOUCH_END,this.onLostItem,this);
        this.btn_depot.node.on(cc.Node.EventType.TOUCH_END,this.onDepot,this);
    }
}
